#include <SDL/SDL.h>
//#pragma comment(lib,"SDL.lib")
//#pragma comment(lib,"SDLmain.lib")
int FilterEvents(const SDL_Event*event){
    if(event->type==SDL_QUIT){
	   printf("SDL_QUIT\n");
	   return 1;
	}else
	if(event->type==SDL_MOUSEMOTION){
       //printf("Mouse Pos %d,%d\n",event->motion.x,event->motion.y);
	   return 0;
	}else
	if(event->type==SDL_MOUSEBUTTONDOWN){
	   printf("Mouse Down\n");
	   return 0;
	}else
	if(event->type==SDL_MOUSEBUTTONUP){
	   printf("Mouse Up\n");
	   return 0;
	}
	return 1;
}
typedef struct _YUVINFO{
FILE*fp;
int width;
int height;
SDL_Overlay*lay;
SDL_Rect rect;
}YUVINFO;
Uint32 SDL_YuvCallback(Uint32 interval, void *param){
	YUVINFO*info=(YUVINFO*)param;
	if(fread(info->lay->pixels[0],1,info->width*info->height*3/2,info->fp)!=0){
	   //fread(info->lay->pixels[1],1,info->width*info->height/4,info->fp);
	   //fread(info->lay->pixels[2],1,info->width*info->height/4,info->fp);
	   SDL_DisplayYUVOverlay(info->lay,&info->rect);
	}else{
	   SDL_Event event;
	   event.type=SDL_QUIT;
	   SDL_PushEvent(&event);
	}
//返回下次间隔，为0就不会再次执行
	return interval;
}
int PlayYUVBySDL(const char*filename,int width,int height){
	SDL_Surface *screen=NULL;
	SDL_Overlay *yuvlay=NULL;
	SDL_Event event;
	FILE*fp=fopen(filename,"rb");
	SDL_TimerID timerID=NULL;
	YUVINFO yuvinfo;
//初始化视频相关
//SDL_putenv("SDL_VIDEODRIVER=directx");
//最后SDL_Quit释放
	if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER)<0){
		fprintf(stderr,"Couldn't initialize SDL:%s\n",SDL_GetError());
		return 0;
	}
	atexit(SDL_Quit);
	SDL_SetEventFilter(FilterEvents);
//创建一个窗口，设置宽高，色彩位数，使用系统内存还是显卡内存
	screen=SDL_SetVideoMode(352,288,32,SDL_HWSURFACE);
	SDL_Flip(screen);
//使用的驱动
	do 	{
	    char driverName[20]={0};
	    SDL_VideoDriverName(driverName, 20);
	    printf("driverName:%s\n",driverName);
	} while (0);
//视频信息
/*
   do  {
	   const SDL_VideoInfo* myInfo=SDL_GetVideoInfo();
	   if(myInfo!=NULL){
	      printf("HW:%d WM:%s\n",myInfo->hw_available,myInfo->wm_available);
	      printf("Mem:%d\n",myInfo->video_mem);
	   }
   } while (0);
 */
	if(screen==NULL){
	   fprintf(stderr,"Couldn't set 352x288 video mode: %s\n",SDL_GetError());
	   return 0;
	}
//创建YUV信息，捆绑到指定窗口，根据YUV具体格式设置类型
	yuvlay=SDL_CreateYUVOverlay(352,288,SDL_IYUV_OVERLAY,screen);
	if(yuvlay==NULL){
	   fprintf(stderr,"Couldn't set yuv mode: %s\n",SDL_GetError());
	}else{
	   printf("w:%d h:%d planes:%d %d %d %d\n",yuvlay->w,yuvlay->h,yuvlay->planes,
	   yuvlay->pitches[0],yuvlay->pitches[1],yuvlay->pitches[2]);
	}
//构建相关信息
	yuvinfo.rect.x=0;
	yuvinfo.rect.y=0;
	yuvinfo.rect.w=width;
	yuvinfo.rect.h=height;
	yuvinfo.fp=fp;
	yuvinfo.height=height;
	yuvinfo.width=width;
	yuvinfo.lay=yuvlay;
	timerID=SDL_AddTimer(40,SDL_YuvCallback,&yuvinfo);
	if(timerID==NULL)printf("创建失败\n");
//按YUV存储方式读取数据
    while(SDL_PollEvent(&event)>=0){
	  if(event.type==SDL_QUIT){
	     printf("SDL_QUIT222\n");
	     break;
      }		
	}
	SDL_RemoveTimer(timerID);
//释放窗口
	if(screen)SDL_FreeSurface(screen);
//释放YUV数据
	if(yuvlay)SDL_FreeYUVOverlay(yuvlay);
//释放其它
	SDL_Quit();
	fclose(fp);
    return 0;
}